﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class RulerEffectForm : ItemFormTemplate
    {
        public RulerEffectForm()
        {
            InitializeComponent();
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {

        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        bool mousedownobject = false;
        Point mousedownpoint = Point.Empty;
        Point mousedowncontrolpoint = Point.Empty;

        private void Form_Load(object sender, EventArgs e)
        {
            this.demo_piece_panel.Size = new Size(100, 100);
            this.demo_hRuler.ZoomScaling = 1;
            this.demo_hRuler.Mark.MainLinePosition = this.demo_piece_panel.Location.X;
            this.demo_vRuler.ZoomScaling = 1;
            this.demo_vRuler.Mark.MainLinePosition = this.demo_piece_panel.Location.Y;
            this.demo_piece_panel.MouseWheel += this.demo_piece_panel_MouseWheel;
            this.demo_container_panel.MouseWheel += this.demo_piece_panel_MouseWheel;
            this.demo_piece_panel.MouseDown += this.demo_piece_panel_MouseDown;
            this.demo_piece_panel.MouseUp += this.demo_piece_panel_MouseUp;
            this.demo_piece_panel.MouseMove += this.demo_piece_panel_MouseMove;
        }

        private void demo_piece_panel_MouseWheel(object sender, MouseEventArgs e)
        {
            Size si = this.demo_piece_panel.Size;
            int value = e.Delta / 120 * 4;
            Size tmp = new Size(this.demo_piece_panel.Width + value, this.demo_piece_panel.Height + value);
            tmp = new Size(Math.Max(100, tmp.Width), Math.Max(100, tmp.Height));
            tmp = new Size(Math.Min(400, tmp.Width), Math.Min(400, tmp.Height));


            this.demo_piece_panel.Size = tmp;
            this.demo_piece_panel.Location = new Point(Math.Max(0, this.demo_piece_panel.Location.X - (si.Width - tmp.Width) / 2), Math.Max(0, this.demo_piece_panel.Location.Y - (si.Height - tmp.Height) / 2));
            this.demo_hRuler.Mark.MainLinePosition = this.demo_piece_panel.Location.X;
            this.demo_vRuler.Mark.MainLinePosition = this.demo_piece_panel.Location.Y;
            this.demo_hRuler.ZoomScaling = tmp.Width / 100f;
            this.demo_vRuler.ZoomScaling = tmp.Height / 100f;
        }

        private void demo_piece_panel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.mousedownobject = true;
                mousedownpoint = Control.MousePosition;
                mousedowncontrolpoint = this.demo_piece_panel.Location;
            }
        }

        private void demo_piece_panel_MouseUp(object sender, MouseEventArgs e)
        {
            this.mousedownobject = false;
            mousedownpoint = Point.Empty;
            mousedowncontrolpoint = Point.Empty;
        }

        private void demo_piece_panel_Leave(object sender, EventArgs e)
        {
            this.mousedownobject = false;
            mousedownpoint = Point.Empty;
            mousedowncontrolpoint = Point.Empty;
        }

        private void demo_piece_panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && mousedownobject)
            {
                Point current = Control.MousePosition;

                Point tmp = new Point(this.mousedowncontrolpoint.X + current.X - this.mousedownpoint.X, this.mousedowncontrolpoint.Y + current.Y - this.mousedownpoint.Y);

                tmp = new Point(Math.Max(0, tmp.X), Math.Max(0, tmp.Y));

                tmp = new Point(Math.Min(this.demo_container_panel.Width - this.demo_piece_panel.Width, tmp.X), Math.Min(this.demo_container_panel.Height - this.demo_piece_panel.Height, tmp.Y));

                this.demo_piece_panel.Location = tmp;
                this.demo_hRuler.Mark.MainLinePosition = tmp.X;
                this.demo_vRuler.Mark.MainLinePosition = tmp.Y;
            }
            if (this.demo_hRuler.Mark.HighlightLineVisible)
            {
                this.demo_hRuler.SetHighlightLinePosition(this.demo_hRuler.PointToClient(MousePosition).X);
            }
            if (this.demo_vRuler.Mark.HighlightLineVisible)
            {
                this.demo_vRuler.SetHighlightLinePosition(this.demo_vRuler.PointToClient(MousePosition).Y);
            }
        }

        private void demo_container_panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.demo_hRuler.Mark.HighlightLineVisible)
            {
                this.demo_hRuler.SetHighlightLinePosition(this.demo_hRuler.PointToClient(MousePosition).X);
            }
            if (this.demo_vRuler.Mark.HighlightLineVisible)
            {
                this.demo_vRuler.SetHighlightLinePosition(this.demo_vRuler.PointToClient(MousePosition).Y);
            }
        }

        #endregion
    }
}
